package me.xADudex.RandomLocation;

import org.bukkit.inventory.*;
import java.util.*;

public class StringTools
{
    public static String arrayToString(final Object[] array, final String seperator) {
        String string = "";
        for (final Object obj : array) {
            string = String.valueOf(string) + seperator + obj.toString();
        }
        string = string.replaceFirst(seperator, "");
        return string;
    }

    public static boolean getBoolean(final String s) {
        return s.equalsIgnoreCase("true");
    }

    public static int getInteger(final String s) {
        try {
            return Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            return 0;
        }
    }

    public static double getDouble(final String s) {
        try {
            return Double.parseDouble(s);
        }
        catch (NumberFormatException e) {
            return 0.0;
        }
    }

    public static Integer[] getIntArray(final String s, final String split) {
        final String[] raw = s.split(split);
        final ArrayList<Integer> list = new ArrayList<Integer>();
        String[] array;
        for (int length = (array = raw).length, i = 0; i < length; ++i) {
            final String arg = array[i];
            if (isInt(arg)) {
                list.add(getInteger(arg));
            }
        }
        return list.toArray(new Integer[list.size()]);
    }

    public static boolean isIntArray(final String s, final String split) {
        String[] split2;
        for (int length = (split2 = s.split(split)).length, i = 0; i < length; ++i) {
            final String arg = split2[i];
            if (!isInt(arg)) {
                return false;
            }
        }
        return true;
    }

    public static BlockData[] getBlockDataArray(final String s, final String split) {
        final ArrayList<BlockData> list = new ArrayList<BlockData>();
        String[] split2;
        for (int length = (split2 = s.split(split)).length, i = 0; i < length; ++i) {
            final String arg = split2[i];
            list.add(new BlockData(arg));
        }
        return list.toArray(new BlockData[list.size()]);
    }

    public static boolean isBoolean(final String s) {
        return s.equalsIgnoreCase("false") || s.equalsIgnoreCase("true");
    }

    public static boolean isInt(final String s) {
        try {
            Integer.parseInt(s);
            return true;
        }
        catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isDouble(final String s) {
        try {
            Double.parseDouble(s);
            return true;
        }
        catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean arrayContains(final Object[] array, final Object obj) {
        for (final Object arg : array) {
            if (arg.equals(obj)) {
                return true;
            }
        }
        return false;
    }

    public static String itemToString(final ItemStack item) {
        String map = item.serialize().toString();
        map = (String.valueOf(map.replaceFirst("\\{", "")) + "§§§§§").replaceAll("}§§§§§", "").replaceAll(" ", "");
        return map;
    }

    public static ItemStack getItemStack(final String s) {
        try {
            final Map<String, Object> imap = new HashMap<String, Object>();
            String[] split;
            for (int length = (split = s.split(",")).length, i = 0; i < length; ++i) {
                final String key = split[i];
                imap.put(key.split("=")[0], key.replaceFirst(String.valueOf(key.split("=")[0]) + "=", ""));
            }
            final Iterator<String> iterator = imap.keySet().iterator();
            while (iterator.hasNext()) {
                final String key = iterator.next();
                if (imap.get(key).toString().replaceAll("[0-9]", "").equals("")) {
                    imap.put(key, Integer.parseInt(imap.get(key).toString()));
                }
                else {
                    if (!imap.get(key).toString().replaceAll("[0-9]", "").equals("\\.")) {
                        continue;
                    }
                    imap.put(key, Double.parseDouble(imap.get(key).toString()));
                }
            }
            return ItemStack.deserialize((Map)imap);
        }
        catch (Exception e) {
            return null;
        }
    }

    public static String mergeStrings(final int totalLength, final int spaceLength, final String splitter, final String... args) {
        final int subLength = (totalLength - spaceLength * (args.length - 1)) / args.length;
        int longest = 0;
        final ArrayList<String[]> lists = new ArrayList<String[]>();
        for (final String key : args) {
            final String[] array = key.split(splitter);
            lists.add(array);
            final int keyLength = array.length;
            if (keyLength > longest) {
                longest = keyLength;
            }
        }
        String space = "";
        String keySpace = "";
        for (int i = 0; i < spaceLength; ++i) {
            space = String.valueOf(space) + " ";
        }
        for (int i = 0; i < subLength; ++i) {
            keySpace = String.valueOf(keySpace) + " ";
        }
        String total = "";
        for (int j = 0; j < longest; ++j) {
            total = String.valueOf(total) + "\n";
            for (int li = 0; li < lists.size(); ++li) {
                final String[] list = lists.get(li);
                if (li != lists.size() - 1) {
                    if (list.length > j) {
                        String key2 = list[j];
                        if (key2.length() > subLength) {
                            key2 = String.valueOf(key2.substring(0, subLength - 1)) + "§";
                        }
                        if (key2.length() < subLength) {
                            for (int num = key2.length(); num < subLength; ++num) {
                                key2 = String.valueOf(key2) + " ";
                            }
                        }
                        total = String.valueOf(total) + key2 + space;
                    }
                    else {
                        total = String.valueOf(total) + keySpace + space;
                    }
                }
                else if (list.length > j) {
                    String key2 = list[j];
                    if (key2.length() > subLength) {
                        key2 = String.valueOf(key2.substring(0, subLength - 1)) + "§";
                    }
                    if (key2.length() < subLength) {
                        for (int num = key2.length(); num < subLength; ++num) {
                            key2 = String.valueOf(key2) + " ";
                        }
                    }
                    total = String.valueOf(total) + key2;
                }
                else {
                    total = String.valueOf(total) + keySpace + space;
                }
            }
        }
        return total.replaceFirst("\n", "");
    }
}
